package com.atguigu.educms.service.impl;

import com.atguigu.common.cron.bean.FrontPageBean;
import com.atguigu.common.cron.bean.PageListBean;
import com.atguigu.common.cron.vo.ContentQueryVo;
import com.atguigu.educms.entity.CmsContent;
import com.atguigu.educms.mapper.CmsContentMapper;
import com.atguigu.educms.service.CmsContentService;
import com.atguigu.educms.util.PageFrontUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文章详情表 服务实现类
 * </p>
 *
 * @author Panghl
 * @since 2021-03-11
 */
@Service
public class CmsContentServiceImpl extends ServiceImpl<CmsContentMapper, CmsContent> implements CmsContentService {

    @Override
    public PageListBean pageContentConditions(Integer pageNo, Integer pageSize, ContentQueryVo contentQueryVo) {
        Page<CmsContent> pageParam = new Page<>(pageNo, pageSize);
        QueryWrapper<CmsContent> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(contentQueryVo)) {
            if (!StringUtils.isEmpty(contentQueryVo.getContentTitle())){
                qw.like("content_title", contentQueryVo.getContentTitle());
            }
            if (!StringUtils.isEmpty(contentQueryVo.getContentType())){
                qw.eq("content_type", contentQueryVo.getContentType());
            }
            if (!StringUtils.isEmpty(contentQueryVo.getStartDate())&&!StringUtils.isEmpty(contentQueryVo.getStartDate())){
                qw.between("gmt_create",contentQueryVo.getStartDate(),contentQueryVo.getEndDate());
            }
        }
        qw.orderByDesc("gmt_create");
        baseMapper.selectPage(pageParam, qw);
        return new PageListBean((int) pageParam.getTotal(), pageParam.getRecords());
    }

    @Override
    public FrontPageBean getContentList(Integer pageNo, Integer pageSize) {
        Page<CmsContent> pageParam = new Page<>(pageNo, pageSize);
        QueryWrapper<CmsContent> qw = new QueryWrapper<>();
        qw.orderByDesc("gmt_create");
        baseMapper.selectPage(pageParam, qw);
        return PageFrontUtils.copyPropertiesFrontPage(pageParam);
    }

    @Override
    public List<CmsContent> getHotContent() {
        QueryWrapper<CmsContent> qw = new QueryWrapper<>();
        qw.orderByDesc("content_comment", "gmt_create");
        qw.last("limit 8");
        return baseMapper.selectList(qw);
    }

    @Override
    public void editCommentNumById(Integer contentId, Integer commentNum) {
        baseMapper.updateNumById(contentId,commentNum);
    }


}
